﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using BusinessLayer.WorldArtPrice.Entities;
using DBHelper;

namespace BusinessLayer.WorldArtPrice
{
    public class MetaHeaderBL
    {
        private readonly DbHelper _db;

        public MetaHeaderBL()
        {
            _db = new DbHelper(new DbConfig().DbWorldArtPriceConnectionString);         
        }

        public MetaHeader GetByUrl(string url)
        {
            var oCommand = new SqlCommand("sp_MetaHeader_Select_By_Url") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Url", url));            
                                                        
            return _db.GetList<MetaHeader>(oCommand).FirstOrDefault();
           
        }
        public List<MetaHeader> GetAll()
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAllMetaHeaders) { CommandType = CommandType.StoredProcedure };

            var list = _db.GetList<MetaHeader>(oCommand);
            return list;
        }
        public int CreateMetaHeader(MetaHeader metaHeader)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.InsertMetaHeader) { CommandType = CommandType.StoredProcedure };
                oCommand.Parameters.Add(new SqlParameter("@Url", metaHeader.Url));
                oCommand.Parameters.Add(new SqlParameter("@Title", metaHeader.Title));
                oCommand.Parameters.Add(new SqlParameter("@Description", metaHeader.Description));
                oCommand.Parameters.Add(new SqlParameter("@KeyWord", metaHeader.KeyWord));



                result = _db.ExecuteNonQuery(oCommand);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }
        public int UpdateMetaHeader(MetaHeader metaHeader)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.UpdateMetaHeader) { CommandType = CommandType.StoredProcedure };
                oCommand.Parameters.Add(new SqlParameter("@Id", metaHeader.Id));
                oCommand.Parameters.Add(new SqlParameter("@Url", metaHeader.Url));
                oCommand.Parameters.Add(new SqlParameter("@Title", metaHeader.Title));
                oCommand.Parameters.Add(new SqlParameter("@Description", metaHeader.Description));
                oCommand.Parameters.Add(new SqlParameter("@KeyWord", metaHeader.KeyWord));

                result = _db.ExecuteNonQuery(oCommand);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }

        public MetaHeader GetByID(int id)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetMetaHeaderById) { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Id", id));
            var list = _db.GetList<MetaHeader>(oCommand);
            return list.FirstOrDefault();
        }
        public int DeleteMetaHeaderByIds(List<string> lstofids)
        {
            int result = -1;

            string ids = "";
            foreach (var id in lstofids)
            {
                ids += id + ",";
            }
            ids = String.IsNullOrEmpty(ids) ? "" : ids.Substring(0, ids.Length - 1);
            if (!String.IsNullOrEmpty(ids))
            {
                try
                {
                    var oCommand = new SqlCommand(String.Format("DELETE FROM MetaHeader WHERE Id IN ({0});", ids)) { CommandType = CommandType.Text };
                    result = _db.ExecuteNonQuery(oCommand);
                    return result;
                }
                catch (Exception ex)
                {

                }
            }
            return result;
        }
    }
}
